package huixue.cn.com.rxapp.user.ui;

import android.content.Intent;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.github.nukc.stateview.StateView;

import java.util.List;

import butterknife.BindView;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.adapter.DividerGridItemDecoration;
import huixue.cn.com.rxapp.adapter.IndexAdapter;
import huixue.cn.com.rxapp.base.BaseActivity;
import huixue.cn.com.rxapp.base.BasePresent;
import huixue.cn.com.rxapp.entity.Gank;
import huixue.cn.com.rxapp.entity.HttpResult;
import huixue.cn.com.rxapp.user.present.IndexPresent;
import huixue.cn.com.rxapp.ui.PictureActivity;


public class IndexActivity extends BaseActivity<IndexPresent,IndexPresent.IndexUI>
        implements IndexPresent.IndexUI {

    @BindView(R.id.rcv_image)
    RecyclerView mRecycler;
    private IndexAdapter mIndexAdapater;

    private List<Gank> mData;

    private StateView mStateView;

    private IndexPresent mIndexPresent;

    @Override
    public int getLayout() {
        return R.layout.activity_index;
    }

    @Override
    public void addEvent() {
        loadData();
    }

    public void initView() {
        //mIndexPresent = new IndexPresent(this,this);
        StaggeredGridLayoutManager layoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mIndexAdapater = new IndexAdapter(this, null);
        mRecycler.setLayoutManager(layoutManager);
        mRecycler.addItemDecoration(new DividerGridItemDecoration(this,R.drawable.divider_shape));
        mIndexAdapater.setmItemClickListener(new IndexAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position,View view) {
                Gank gank = mData.get(position);
                Intent intent = PictureActivity.newIntent(IndexActivity.this, gank.url, gank.desc);
                ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
                        IndexActivity.this, view, PictureActivity.TRANSIT_PIC);
                try {
                    ActivityCompat.startActivity(IndexActivity.this, intent, optionsCompat.toBundle());
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                    IndexActivity.this.startActivity(intent);
                }
            }
        });
        mRecycler.setAdapter(mIndexAdapater);

        mStateView = StateView.inject(findViewById(R.id.fl_content));
        mStateView.setRetryResource(R.layout.page_net_error);
        mStateView.setOnRetryClickListener(new StateView.OnRetryClickListener() {
            @Override
            public void onRetryClick() {
                loadData();
            }
        });

        //mIndexAdapater.notifyItemInserted();
    }



    private void loadData() {
        mIndexPresent.loadData();
    }

    @Override
    public void loadImageSuccess(HttpResult<List<Gank>> listHttpResult) {
        mStateView.showContent();
        //mData = listHttpResult.getResults();
        //mIndexAdapater.setmList(listHttpResult.getResults());
        //mIndexAdapater.notifyDataSetChanged();
    }

    /*@Override
    public void onFail() {
        mStateView.showRetry();
    }

    @Override
    public void onSuccess() {

    }*/

    @Override
    protected IndexPresent createPresenter() {
        return new IndexPresent();
    }

    @Override
    protected IndexPresent.IndexUI getUI() {
        return null;
    }
}